"""
自动清理 Mock 测试用户（无需确认）
删除所有 wechat_openid 以 'mock_' 开头的测试用户
"""
import sys
import os

# 添加项目根目录到 Python 路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.core.database import SessionLocal
from app.models.user import User
from app.core.logger import logger


def clean_mock_users_auto():
    """自动清理 Mock 测试用户"""
    db = SessionLocal()
    
    try:
        logger.info("🔍 开始查找 Mock 测试用户...")
        
        # 查找所有 Mock 用户
        mock_users = db.query(User).filter(
            User.wechat_openid.like('mock_%')
        ).all()
        
        if not mock_users:
            logger.info("✅ 没有找到 Mock 测试用户")
            return 0
        
        logger.info(f"📋 找到 {len(mock_users)} 个 Mock 测试用户")
        
        # 删除用户
        delete_count = 0
        for user in mock_users:
            try:
                logger.info(f"🗑️  删除: {user.nick_name} ({user.wechat_openid})")
                db.delete(user)
                delete_count += 1
            except Exception as e:
                logger.error(f"❌ 删除用户失败: {user.id}, 错误: {e}")
        
        db.commit()
        logger.success(f"\n✅ 成功删除 {delete_count} 个 Mock 测试用户！")
        return delete_count
        
    except Exception as e:
        logger.error(f"❌ 清理失败: {e}")
        db.rollback()
        raise
    finally:
        db.close()


if __name__ == "__main__":
    print("="*60)
    print("🧹 自动清理 Mock 测试用户")
    print("="*60)
    print()
    
    deleted = clean_mock_users_auto()
    
    print()
    print("="*60)
    print(f"✅ 清理完成！共删除 {deleted} 个测试用户")
    print("="*60)

